﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DHQGHN.DAL.BO.Admin;

namespace DHQGHN.MVC.Controllers
{
    public class LogStream
    {
        private static LogBO objBO = new LogBO();
        static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(LogStream));
        public static bool Write(string account, string software, string function, string data, string action, string appCode = "TKTHDS.PM0")
        {
            try
            {
                return objBO.Save(new DAL.Models.Admin.Log
                {
                    Account = account,
                    SoftWare = software,
                    Function = function,
                    Data = data,
                    Action = action,
                    ActionTime = DateTime.Now,
                    AppCode = appCode
                });
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public static bool Write<T>(string account, string software, string function, string action, T dataObject, string appCode = "TKTHDS.PM0")
        {
            try
            {
                string data = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dataObject);

                return objBO.Save(new DAL.Models.Admin.Log
                {
                    Account = account,
                    SoftWare = software,
                    Function = function,
                    Data = data,
                    Action = action,
                    ActionTime = DateTime.Now,
                    AppCode = appCode
                });
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public static string GetJsonString<T>(T data)
        {
            try
            {
                string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data);

                return json;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
    }
}